<template>
  <div class="list">
    <!-- 1.面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">文章管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 2.卡片视图 -->
    <el-card class="box-card" style="margin-top: 15px">
      <!-- 3.表格 -->
      <el-table :data="tableData" border style="width=100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="封面" width="100">
          <!-- 自定义列表可以展示文本之外的信息(图片...) -->
          <template slot-scope="scope">
            <!-- {{ scope.row }} -->
            <img
              class="cover"
              v-if="scope.row.cover.length"
              :src="scope.row.cover[0].url | imgUrl"
            />
          </template>
        </el-table-column>
        <el-table-column label="文章类型">
          <!-- 自定义列表可以展示文本之外的信息(图片...) -->
          <template slot-scope="scope">
            <!-- {{ scope.row }} -->
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>

        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- {{ scope.row }} -->
            <el-button
              size="mini"
              @click="$router.push(`/home/post_publish/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 4.分页 -->
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[4, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getPostAPI } from "@/api/news";
export default {
  data() {
    return {
      tableData: [],
      pageIndex: 1, //当前被选中的页码
      pageSize: 4,
      total: 0,
    };
  },
  async created() {
    // 获取文章列表
    // 获取当前数据的总数：因为后台接口并没有返回数据的总数或者总页数
    const total = (await getPostAPI({ pageSize: 1000 })).data.data.length;
    this.total = total;
    console.log(total);
    //获取数据
    this.getPostList();
  },
  methods: {
    //获取当前页的数据
    async getPostList() {
      // 获取文章列表的第一页
      // 我们一定要使用data中定义的pageIndex和pageSize发起数据请求
      const res = await getPostAPI({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      console.log(res);
      this.tableData = res.data.data;
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getPostList();
      //页码重置
      this.pageIndex = 1;
    },
    handleCurrentChange(val) {
      console.log("页码被改变了");
      this.pageIndex = val;
      this.getPostList();
    },

    handleDelete() {
      console.log();
    },
  },
};
</script>

<style lang="less" scoped>
.cover {
  width: 50px;
  height: 50px;
}
.page {
  margin-top: 20px;
  text-align: center;
}
</style>